import axios from 'axios'
import { Toast } from 'vant'
import bridge from '@/utils/dsbridge.js'
import Cookies from 'js-cookie'
// const token = Cookies.get('token')
import qs from 'qs'
import { getValue, setValue } from '@/utils/auth'
// getUrlKey('token')
// const token = 'eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiIxNTc2MDU4Nzk3NTE1Iiwic3ViIjoiMTMwMTExMTExMTEiLCJhdWQiOiIxMzAxMTExMTExMSBNb3ppbGxhLzUuMCAoTGludXg7IEFuZHJvaWQgNS4wOyBTTS1HOTAwUCBCdWlsZC9MUlgyMVQpIEFwcGxlV2ViS2l0LzUzNy4zNiAoS0hUTUwsIGxpa2UgR2Vja28pIENocm9tZS83OC4wLjM5MDQuMTA4IE1vYmlsZSBTYWZhcmkvNTM3LjM2IiwiaWF0IjoxNTc2MDU4Nzk3LCJleHAiOjE1NzYwODc1OTcsImN1c3RvbUF0dHIiOiI4MTAxOTEyMDkwMDAwMDEifQ.rXy1eYyDBNiqXeY63HITJRQoVy7pSX5lNAYcJzS7TRs'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 500000 // request timeout
})
// request interceptor
service.interceptors.request.use(

  (config) => {
    if (!config.params) {
      config.params = {}
    }
    config.params.subjectInfoId = getValue('subjectInfoId') || 1
    config.params.tenantCode = getValue('tenantCode') || 'T000010'
    // config.params.callChannel = '1'
    config.params.loginUserId = getValue('loginUserId')
    // config.params.roleInfos = getValue("roleInfos");
    // config.params.LOGIN = getValue("LOGIN");
    // config.params.loginName = getValue("loginName");
    // config.params.SECURELOGIN = getValue("SECURELOGIN");
    // token
    // config.params.channel = 1
    // if (config.method === "post") {
    //   config.data = qs.stringify(config.data);
    // }
    config.headers.token = getValue('token') || ''
    // config.headers['Content-Type'] = 'application/json;charset=UTF-8';
    // config.url = config.baseURL + config.url;
    return config
  },
  (error) => {
    // do something with request error
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    if (
      [
        'application/excel',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      ].indexOf(response.headers['content-type']) !== -1
    ) {
      return response
    }
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if (res.status == '0') {
    //   if (
    //     (res.header.retCode === "200006" || res.header.retCode === "200007") &&
    //     window.location.pathname !== "/login"
    //   ) {
    //     Toast(res.header.retMsg);
    //   } else if (res.header.retCode === "300000") {
    //     // 工作流不能查看
    //     return res;
    //   } else if (res.header.retCode === "200001") {
    //     bridge.call("login");
    //     return false;
    //   } else {
    //     Toast(res.header.retMsg);
    //     return Promise.reject(res.header.retMsg);
    //   }
    // } else {
    //   const token = res.header.token;
    //   if (token) {
    //     setValue("token", token);
    //   }
      return res
    } else {
      return res
    }
  },
  (error) => {
    // Message({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    return Promise.reject(error)
  }
)

export default service
